package cn.iocoder.yudao.module.system.service.roleweixinmenu;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.controller.admin.roleweixinmenu.vo.RoleWeixinMenuPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.roleweixinmenu.vo.RoleWeixinMenuSaveReqVO;
import cn.iocoder.yudao.module.system.controller.admin.roleweixinmenu.vo.RoleWeixinMenuSaveReqVO2;
import cn.iocoder.yudao.module.system.dal.dataobject.roleweixinmenu.RoleWeixinMenuDO;
import cn.iocoder.yudao.module.system.dal.mysql.roleweixinmenu.RoleWeixinMenuMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.ROLE_WEIXIN_MENU_NOT_EXISTS;

/**
 * 微信菜单和角色关联 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class RoleWeixinMenuServiceImpl implements RoleWeixinMenuService {

    @Resource
    private RoleWeixinMenuMapper roleWeixinMenuMapper;

    @Override
    public Long createRoleWeixinMenu(RoleWeixinMenuSaveReqVO createReqVO) {
        // 插入
        RoleWeixinMenuDO roleWeixinMenu = BeanUtils.toBean(createReqVO, RoleWeixinMenuDO.class);
        // 给默认值
        roleWeixinMenu.setDeleted(false);
        roleWeixinMenuMapper.insert(roleWeixinMenu);
        // 返回
        return roleWeixinMenu.getId();
    }

    @Override
    public Long saveWeinxinMenu(RoleWeixinMenuSaveReqVO2 createReqVO) {
        roleWeixinMenuMapper.deleteByRoleId(createReqVO.getRoleId());

       String arr [] =  createReqVO.getWeixinMenuIds().split(",");
        for (int i = 0; i < arr.length; i++) {
            RoleWeixinMenuDO ddo = new RoleWeixinMenuDO();
            ddo.setDeleted(false);
            ddo.setWeixinMenuId(Long.parseLong(arr[i]));
            ddo.setRoleId(createReqVO.getRoleId());
            roleWeixinMenuMapper.insert(ddo  );

        }

        return 0L;
    }

    @Override
    public void updateRoleWeixinMenu(RoleWeixinMenuSaveReqVO updateReqVO) {
        // 校验存在
        validateRoleWeixinMenuExists(updateReqVO.getId());
        // 更新
        RoleWeixinMenuDO updateObj = BeanUtils.toBean(updateReqVO, RoleWeixinMenuDO.class);
        roleWeixinMenuMapper.updateById(updateObj);
    }

    @Override
    public void deleteRoleWeixinMenu(Long id) {
        // 校验存在
        validateRoleWeixinMenuExists(id);
        // 删除
        roleWeixinMenuMapper.deleteById(id);
    }

    @Override
    public void deleteRoleWeixinMenuListByIds(List<Long> ids) {
        // 校验存在
        validateRoleWeixinMenuExists(ids);
        // 删除
        roleWeixinMenuMapper.deleteByIds(ids);
    }

    private void validateRoleWeixinMenuExists(List<Long> ids) {
        List<RoleWeixinMenuDO> list = roleWeixinMenuMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(ROLE_WEIXIN_MENU_NOT_EXISTS);
        }
    }

    private void validateRoleWeixinMenuExists(Long id) {
        if (roleWeixinMenuMapper.selectById(id) == null) {
            throw exception(ROLE_WEIXIN_MENU_NOT_EXISTS);
        }
    }

    @Override
    public RoleWeixinMenuDO getRoleWeixinMenu(Long id) {
        return roleWeixinMenuMapper.selectById(id);
    }

    @Override
    public PageResult<RoleWeixinMenuDO> getRoleWeixinMenuPage(RoleWeixinMenuPageReqVO pageReqVO) {
        return roleWeixinMenuMapper.selectPage(pageReqVO);
    }

    @Override
    public List<RoleWeixinMenuDO> list(RoleWeixinMenuDO roleWeixinMenuDO) {
        return roleWeixinMenuMapper.selectList(roleWeixinMenuDO);
    }

}